package com.itheima.myiotest8;

import java.io.*;
import java.util.Scanner;

public class Test5 {
    public static void main(String[] args) throws IOException {
        /*
        需求：写一个登陆小案例（添加锁定账号功能）

        步骤：
        	将正确的用户名和密码手动保存在本地的userinfo.txt文件中。
        	保存格式为:username=zhangsan&password=123&count=0
        	让用户键盘录入用户名和密码
        	比较用户录入的和正确的用户名密码是否一致
        	如果一致则打印登陆成功
        	如果不一致则打印登陆失败，连续输错三次被锁定

        */

        //1.把文件中的用户名和密码读入到内存当中
        BufferedReader br = new BufferedReader(new FileReader("myiotest\\src\\com\\itheima\\myiotest8\\userinfo.txt"));
        String line = br.readLine();
        br.close();

        //2.对数据进行处理
        String[] arr = line.split("&");
        String userName = (arr[0].split("="))[1];
        String passWord = (arr[1].split("="))[1];
        int count = Integer.parseInt((arr[2].split("="))[1]);

        //3.键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String InputUserName = sc.nextLine();
        System.out.println("请输入密码：");
        String InputPassWord = sc.nextLine();

        //4.判断
        if (userName.equals(InputUserName) && passWord.equals(InputPassWord)) {
            System.out.println("登陆成功");
            writeFile("username=" + userName + "&password=" + passWord + "&count=0");
        } else {
            count++;
            if (count >= 3) {
                System.out.println("您的账号已被锁定");
            } else {
                System.out.println("登录失败，您还剩" + (3 - count) + "次机会");
            }
            writeFile("username=" + userName + "&password=" + passWord + "&count=" + count);
        }
    }

    public static void writeFile(String content) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("myiotest\\src\\com\\itheima\\myiotest8\\userinfo.txt"));
        bw.write(content);
        bw.newLine();
        bw.close();
    }
}
